﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TheDrinksApp.ViewModel;
using TheDrinksApp.Models;
using System.Collections.ObjectModel;
using TheDrinksApp.Database;
using TheDrinksApp.Screen.Home;

namespace TheDrinksApp.Screen
{
    public partial class SearchScreen : UserControl
    {
        private MainPage mainPage;
        private ObservableCollection<Drink> listDrink = new ObservableCollection<Drink>();
        public ObservableCollection<Drink> ListDrink
        {
            get { return listDrink; }
            set { listDrink = value; }
        }
        private void LoadList(string keyword)
        {
            if (spListDrink.Children.Count > 0)
            {
                spListDrink.Children.Clear();
            }

            List<Drink> r = null;
            if (keyword != null)
            {
                r = ListDrink.Where((item) => item.DrinkName.ToLower().StartsWith(keyword.ToLower())
                                                || item.DrinkName.ToLower().Contains(keyword.ToLower())).ToList();
            }
            else
            {
                r = ListDrink.ToList();
            }

            for (int i = 0; i < r.Count; i++)
            {
                ItemSearch item = new ItemSearch(r[i]);
                item.ShowOrderScreen += OnShowOrderScreen;
                item.ShowMixerScreen += OnShowMixerScreen;

                spListDrink.Children.Add(item);
            }

        }

        private void OnShowOrderScreen(Drink d)
        {
            Util.currentOrder.drink = d;
            mainPage.ShowPage(PAGE.ORDER_SCREEN);
        }

        private void OnShowMixerScreen(Drink d)
        {
            Util.currentOrder.drink = d;
            mainPage.ShowPage(PAGE.MIXER_SCREEN);
        }

        public SearchScreen(MainPage main)
        {
            InitializeComponent();
            mainPage = main;
        }


        private void txtSearchKey_TextChanged(object sender, TextChangedEventArgs e)
        {
            LoadList(txtSearchKey.Text);
        }

        private void txtSearchKey_GotFocus(object sender, RoutedEventArgs e)
        {
            tbLabel.Visibility = Visibility.Collapsed;
        }

        private void txtSearchKey_LostFocus(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(txtSearchKey.Text))
            {
                tbLabel.Visibility = Visibility.Visible;
            }
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            listDrink = Util.GetDrinksByCatergoryID(Util.currentOrder.category.CatergoryID);
            LoadList(null);
        }

        private void tbLabel_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            txtSearchKey.Focus();
            e.Handled = true;
        }
    }
}
